﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EpcTagLibrary
{
    public class GID : EPC
    {
        private long m_ManagerNumber;
        private long m_ObjectClassNumber;
        private long m_SerialNumber;

        public GID(Bits bits)
            : base(bits)
        {
            Divide();
        }

        public GID(byte[] bytes)
            : base(bytes)
        {
            Divide();
        }

        private void Divide()
        {
            m_ManagerNumber = Common.BitsToInt(GetFieldBits(1));
            m_ObjectClassNumber = Common.BitsToInt(GetFieldBits(2));
            m_SerialNumber = Common.BitsToInt(GetFieldBits(3));
        }

        public long ManagerNumber
        {
            get
            {
                return m_ManagerNumber;
            }
        }

        public long ObjectClassNumber
        {
            get
            {
                return m_ObjectClassNumber;
            }
        }

        public long SerialNumber
        {
            get
            {
                return m_SerialNumber;
            }
        }

        public override int[] GetFieldSplits()
        {
            int[] Result = new int[3];
            Result[0] = 88;
            Result[1] = 60;
            Result[2] = 36;

            return Result;
        }

        public override Uri GetTag()
        {
            StringBuilder Result = new StringBuilder();

            Result.Append("urn:epc:id:");
            Result.Append(GetSchemeName());
            Result.Append(":");
            Result.Append(m_ManagerNumber);
            Result.Append(".");
            Result.Append(m_ObjectClassNumber);
            Result.Append(".");
            Result.Append(m_SerialNumber);

            return new Uri(Result.ToString());
        }

        public override Uri GetEPC()
        {
            StringBuilder Result = new StringBuilder();

            Result.Append("urn:epc:tag:");
            Result.Append(GetScheme());
            Result.Append(":");
            Result.Append(m_ManagerNumber);
            Result.Append(".");
            Result.Append(m_ObjectClassNumber);
            Result.Append(".");
            Result.Append(m_SerialNumber);

            return new Uri(Result.ToString());
        }

        public override string GetScheme()
        {
            return GetSchemeName() + "-" + GetTagBitLength();
        }

        public override string GetSchemeName()
        {
            return "gid";
        }
    }
}
